package com.fita.entity.client;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.annotations.DynamicUpdate;
import com.demo2do.core.utils.DateFormatUtils;
import com.fita.entity.agent.Agent;
import com.fita.entity.principal.Principal;

/**
 * @description:
 * @author: 11200
 * @date: 2019/6/20
 */
@Entity
@Table(name = "client")
@DynamicUpdate
public class Client {

    @Id
    @GeneratedValue
    private Long id;

    private String email;

    /**
     * 联系人
     */
    private String contacts;

    @Column(unique = true)
    private String phone;

    @Enumerated(EnumType.STRING)
    @ElementCollection(targetClass = ClientType.class)
    @CollectionTable(name = "client_client_type",
            joinColumns = @JoinColumn(name = "client_id"))
    @Column(name = "client_type", nullable = false)
    @OrderColumn(name = "list_index")
    private List<ClientType> types;

    private String companyName;

    /**
     * 备注
     */
    private String remarks;

    /**
     * 录入人
     */
    @ManyToOne
    @JoinColumn(nullable = false)
    private Principal principal;

    @ManyToOne
    @JoinColumn(nullable = true)
    private Agent agent;

    private Date createTime;

    @Column(name = "is_disabled")
    private Boolean disabled;

    /**
     * 创建client
     * @param principal
     * @return
     */
    public Client create(Principal principal) {
        this.principal = principal;
        this.createTime = new Date();
        this.disabled = false;
        return this;
    }

    /**
     *
     * @return
     */
    public String getFormatedCreateTime(){
        return DateFormatUtils.format(this.createTime);
    }

    /**
     * 修改
     * @param client
     * @return
     */
    public Client edit(Client client) {
        this.email = client.email;
        this.contacts = client.contacts;
        this.phone = client.phone;
        this.types = client.types;
        this.companyName = client.companyName;
        this.remarks = client.remarks;
        return this;
    }

    /**
     * 绑定代理
     * @param agent
     * @return
     */
    public Client bindAgent(Agent agent) {
        this.agent = agent;
        return this;
    }

    /**
     * 是否有代理
     * @return
     */
    public Boolean havingAgent(){
        return agent != null ? true : false;
    }

    /**
     *
     * @return
     */
    public String getAllTypeString () {
        if (CollectionUtils.isNotEmpty(this.types)){
            return this.types.stream().map(ClientType::getDescription).collect(Collectors.joining(" "));
        }else{
            return "";
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getContacts() {
        return contacts;
    }

    public void setContacts(String contacts) {
        this.contacts = contacts;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Principal getPrincipal() {
        return principal;
    }

    public void setPrincipal(Principal principal) {
        this.principal = principal;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public Agent getAgent() {
        return agent;
    }

    public void setAgent(Agent agent) {
        this.agent = agent;
    }

    public List<ClientType> getTypes() {
        return types;
    }

    public void setTypes(List<ClientType> types) {
        this.types = types;
    }
}
